import { createRouter, createWebHistory } from 'vue-router'
import locaCache from '@/utils/locaCache'
import { constRoute } from './routes'
// 引入进度条插件
import NProgress from 'nprogress'
// 引入对应css样式
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constRoute,
  scrollBehavior() {
    return {
      left: 0,
      top: 0
    }
  }
})

// 修改进度条插件的配置
NProgress.configure({
  showSpinner: false
})

router.beforeEach((to) => {
  NProgress.start()
  const token = locaCache.getLocalCache('token')
  if (to.path === '/login' && token) {
    return '/home'
  }
  if (to.path === '/home' && !token) {
    return '/login'
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router
